﻿@model User
@{
    ViewBag.Title = T("Account");
}
<ul class="path floatContainer">
    <li class="first">@Html.ActionLinkLocalized("Forums", "List", "Forums")</li>
</ul>
<h1>@ViewBag.Title</h1>
@Html.ValidationSummary("<h3>" + S("Please check the following errors:") + "</h3>", new Dictionary<string, object>
	{
		{"Email", S("Email format is invalid.")}
		,{"Website", S("Website url format is invalid.")}
		,{"Photo", S("Photo url format is invalid.")}
		,{"Username", S("Username is required.")}
	}, null)
@{Html.BeginForm(null, null, FormMethod.Post, new { @id = "accountForm" });}
@Html.Hidden("gravatarPhoto")
<fieldset>
    <legend>@T("Change your account information")</legend>
    <div class="formItem floatContainer">
		<label for="title">@T("Public name")</label>
		@Html.TextBox("userName", null, new { @class = "text" })
    </div>
    <div class="formItem floatContainer">
		<label for="birthDate">@T("Birthdate")</label>
        @Html.TextBox("birthDate", this.Model.BirthDate != null ? this.Model.BirthDate.Value.ToString("d") : null, new { @class = "text" })
	</div>
	<div class="formItem floatContainer" style="display: none;">
		<label for="timezone">@T("Timezone")</label>
        @Html.TextBox("timezone", null, new { @class = "text" })
    </div>
    <div class="formItem floatContainer">
		<label>@T("Profile image")</label>
		<div class="photo" id="photoPreview"></div>
		<select onchange="return changePhoto(this);">
			<option>@T("Change photo")</option>
			<option value="gravatar">@T("Use gravatar image")</option>
			<option value="remove">@T("Remove photo")</option>
		</select>
	</div>
	<div class="formItem floatContainer" style="display:none;">
		<label for="photo">@T("Photo url")</label>
		@Html.TextBox("photo", null, new { @class = "text" })
    </div>
	<div class="formItem floatContainer">
		<label for="website">@T("Website")</label>
        @Html.TextBox("website", null, new { @class = "text" })
	</div>
	@if (User.AuthenticatedBy(AuthenticationProvider.Membership))
	{
		<p class="button"><input type="button" class="button" value="@T("Change Password")" onclick="window.location.href='@Url.Action("ChangePassword", "FormsAuthentication", new { returnUrl = this.Request.Url.PathAndQuery })';" /></p>
    }
</fieldset>
@if (User.ProviderInfo.AllowChangeEmail)
{
	<h2>@T("Email")</h2>
	<fieldset>
		<legend>@T("Your email will not be shared with anyone. We will not make a different use than detailed below.")</legend>
		<div class="formItem floatContainer">
			<label for="email">@T("Email address")</label>
			@Html.TextBox("email", null, new { @class = "text" })
		</div>
		<div class="formItem floatContainer">
			<div class="checkbox" style="padding-top: 10px;">
				@(Html.CheckBoxBit<EmailPolicy>("policy1", this.Model.EmailPolicy, EmailPolicy.SendFromSubscriptions))
				<label for="policy1">@T("Use my email to notify me of replies to threads I subscribed to")</label>
			</div>
			<div class="checkbox" style="padding-top: 10px;">
				@(Html.CheckBoxBit<EmailPolicy>("policy2", this.Model.EmailPolicy, EmailPolicy.SendNewsletter))
				<label for="policy3">@T("Send me a newsletter")</label>
				@Html.Hidden("emailPolicy")
			</div>
		</div>
	</fieldset>
}
<div class="formItem buttons">
	<input type="submit" value="@T("Submit")" />
</div>
@{Html.EndForm();}
@if (!String.IsNullOrEmpty(User.ProviderInfo.EditAccountUrl))
{
	<p class="action"><a href="@User.ProviderInfo.EditAccountUrl">@T("To edit your account settings, click here")</a></p>
}
<script type="text/javascript">
    $(document).ready(function () {
        $("#accountForm").submit(function () {
            calculateEmailPolicy();
        });
		showPhotoPreview();
    });

    function calculateEmailPolicy() {
        var value = 0;
        $("input:checked", $("#emailPolicy").parents("div.formItem")).each(function () {
            value += parseInt($(this).val(), 10);
        });
        $("#emailPolicy").val(value);
    }

	function showPhotoPreview()
	{
		var photoUrl = $.trim($("#photo").val());
		var img = $("#photoPreview img");
		if (photoUrl != "")
		{
			if (img.length == 0)
			{
				img = $("<img />").appendTo($("#photoPreview"));
			}
			img.prop("src", photoUrl);
		}
		else
		{
			img.remove();
		}
	}

	function changePhoto(sender)
	{
		var value = $(sender).val();
		sender.selectedIndex = 0;
		switch (value)
		{
			case "gravatar":
				assignGravatar();
				break;
			case "remove":
				$("#photo").val("");
				break;
		}
		showPhotoPreview();
		return false;
	}

	function assignGravatar()
	{
		var gravatarPhoto = $("#gravatarPhoto").val();
		if (gravatarPhoto == "")
		{
			alert("You must specify an email address to use Gravatar.");
			return;
		}
		$("#photo").val(gravatarPhoto);
	}
</script>
